<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

class CoreMessage {
	var $subject;
	var $content;
	var $date_added;
	var $status;
	
	var $receiver;
	var $sender;
	
	//constructor
	function CoreMessage() {
		
	}
	
	//setter/getter
	function setReceiver() {
		
	}
	
	function getReceiver() {
		
	}
	
	function setSender() {
		
	}
	
	function getSender() {
		
	}
	
	//methods
}

//
class CoreMessageType {
	var $MT_ORDER = 21;
	var $MT_ORDER_PROCESS = 22;
	
	//
}
//
$CoreMessageType = new CoreMessageType();

///////////////////////////////////////////////////
//	folder
//		1	inbox
//		2	outbox
//		3	draft
//		4	trash
//		5	archive
///////////////////////////////////////////////////

class CoreMessageBase extends Base {
	
	//constructor
	function CoreMessageBase() {
		parent::Base();
		
		$this->owner = $this->_get_current_user();
	}
	
	//methods
	function addMessage($data_array) {
		$data_array = func_db_input($data_array);
		
		$m_type = (isset($data_array['messages_type']) ? $data_array['messages_type'] : $data_array['type']);
		$m_subject = (isset($data_array['messages_subject']) ? $data_array['messages_subject'] : $data_array['subject']);
		$m_content = (isset($data_array['messages_content']) ? $data_array['messages_content'] : $data_array['content']);
		
		//
		$sender = $receiver = array();
		
		//
		if( $data_array['to_system'] == 1 ) {
			//
			if( isset($data_array['sender_nocheck']) ) {
				$sender['id'] = $data_array['sender_id'];
				$sender['name'] = $data_array['sender_name'];
				$sender['email'] = $data_array['sender_email'];
			} elseif( isset($data_array['sender_id']) ) {
				$sender = $this->_get_user($data_array['sender_id'], 'userid');
			} elseif( isset($data_array['sender_name']) ) {
				$sender = $this->_get_user($data_array['sender_name'], 'username');
			} elseif( isset($data_array['sender_email']) ) {
				$sender = $this->_get_user($data_array['sender_email'], 'email');
			} else {
				$sender = array(
						'id' => $_SESSION['suser']->userid,
						'name' => $_SESSION['suser']->username,
						'email' => $_SESSION['suser']->useremail,
							);
			}
			//
			$receiver = $this->_get_system_owner();
		} elseif( $data_array['to_user'] == 1 ) {
			if( $data_array['from_system'] == 1 ) {
				$sender = $this->_get_system_owner();
			} elseif( isset($data_array['sender_id']) && $data_array['sender_id'] > 0 ) {
				$sender['id'] = $data_array['sender_id'];
				$sender['name'] = $data_array['sender_name'];
				$sender['email'] = $data_array['sender_email'];
			} else {
				$sender = array(
						'id' => $_SESSION['suser']->userid,
						'name' => $_SESSION['suser']->username,
						'email' => $_SESSION['suser']->useremail,
							);
			}
			//
			$receiver['id'] = $data_array['receiver_id'];
			$receiver['name'] = $data_array['receiver_name'];
			$receiver['email'] = $data_array['receiver_email'];
		} elseif( isset($data_array['nocheck']) ) {
			//
			if( isset($data_array['sender_nocheck']) ) {
				$sender['id'] = $data_array['sender_id'];
				$sender['name'] = $data_array['sender_name'];
				$sender['email'] = $data_array['sender_email'];
			} elseif( isset($data_array['sender_id']) ) {
				$sender = $this->_get_user($data_array['sender_id'], 'userid');
			} elseif( isset($data_array['sender_name']) ) {
				$sender = $this->_get_user($data_array['sender_name'], 'username');
			} elseif( isset($data_array['sender_email']) ) {
				$sender = $this->_get_user($data_array['sender_email'], 'email');
			} else {
				$sender = array(
						'id' => $_SESSION['suser']->userid,
						'name' => $_SESSION['suser']->username,
						'email' => $_SESSION['suser']->useremail,
							);
			}
			//
			if( isset($data_array['receiver_nocheck']) ) {
				$receiver['id'] = $data_array['receiver_id'];
				$receiver['name'] = $data_array['receiver_name'];
				$receiver['email'] = $data_array['receiver_email'];
			} elseif( isset($data_array['receiver_id']) ) {
				$receiver = $this->_get_user($data_array['receiver_id'], 'userid');
			} elseif( isset($data_array['receiver_name']) ) {
				$receiver = $this->_get_user($data_array['receiver_name'], 'username');
			} elseif( isset($data_array['receiver_email']) ) {
				$receiver = $this->_get_user($data_array['receiver_email'], 'email');
			} else {
				return '-1';
			}
		} else {
			//
			if( isset($data_array['sender_nocheck']) ) {
				$sender['id'] = $data_array['sender_id'];
				$sender['name'] = $data_array['sender_name'];
				$sender['email'] = $data_array['sender_email'];
			} elseif( isset($data_array['sender_id']) ) {
				$sender = $this->_get_user($data_array['sender_id'], 'userid');
			} elseif( isset($data_array['sender_name']) ) {
				$sender = $this->_get_user($data_array['sender_name'], 'username');
			} elseif( isset($data_array['sender_email']) ) {
				$sender = $this->_get_user($data_array['sender_email'], 'email');
			} else {
				$sender = array(
						'id' => $_SESSION['suser']->userid,
						'name' => $_SESSION['suser']->username,
						'email' => $_SESSION['suser']->useremail,
							);
			}
			//
			if( isset($data_array['receiver_nocheck']) ) {
				$receiver['id'] = $data_array['receiver_id'];
				$receiver['name'] = $data_array['receiver_name'];
				$receiver['email'] = $data_array['receiver_email'];
			} elseif( isset($data_array['receiver_id']) ) {
				$receiver = $this->_get_user($data_array['receiver_id'], 'userid');
			} elseif( isset($data_array['receiver_name']) ) {
				$receiver = $this->_get_user($data_array['receiver_name'], 'username');
			} elseif( isset($data_array['receiver_email']) ) {
				$receiver = $this->_get_user($data_array['receiver_email'], 'email');
			} else {
				return '-1';
			}
		}
		if( $this->_not_null($sender) == false ) {
			return '-2';
		}
		if( $this->_not_null($receiver) == false ) {
			return '-3';
		}
		
		//
		$m_sid = $sender['id'];
		$m_s = $sender['name'];
		$m_semail = $sender['email'];
		//
		$m_rid = $receiver['id'];
		$m_r = $receiver['name'];
		$m_remail = $receiver['email'];
		
		//
		$query = "insert into pp_messages set messages_subject = '" . $m_subject . "', "
										. " messages_content = '" . $m_content . "', "
										//
										. " messages_type = '" . (int)$m_type . "', "
										//
										. " messages_senderid = '" . $m_sid . "', "
										. " messages_sender = '" . $m_s . "', "
										. " messages_senderemail = '" . $m_semail . "', "
										//
										. " messages_receiverid = '" . $m_rid . "', "
										. " messages_receiver = '" . $m_r . "', "
										. " messages_receiveremail = '" . $m_remail . "', "
										//
										. " messages_date_added = now(), "
										. " messages_folder = '1', "
										. " messages_isnew = 1, "
										. " messages_status = 1"
										;
		$messages_id = $this->adb->insert($query);
		
		return $messages_id;
	}
	
	function addMessageToSystem($data_array) {
		$data_array['to_system'] = 1;
		
		return $this->addMessage($data_array);
	}
	
	function addMessageFromSystem($data_array) {
		$data_array['to_user'] = 1;
		$data_array['from_system'] = 1;
		
		return $this->addMessage($data_array);
	}
	
	function addMessageA($data_array, $type = 'c2s') {
		$data_array = func_db_input($data_array);
		
		$m_subject = $data_array['subject'];
		$m_content = $data_array['content'];
		
		if( $type == 's2c' ) {
			$sender = $this->_get_system_owner();
			
			global $SETTINGS;
			$m_sid = $sender['id'];
			$m_stype = 'supplier';
			$m_s = $sender['name'];
			$m_semail = $sender['email'];
			$m_scompany = $SETTINGS['bs_company'];
			$m_stelephone = $SETTINGS['bs_telephone'];
			$m_saddress = $SETTINGS['bs_address'];
			//
			if( $data_array['receiver_iscustomer'] == '1' ) {
				if( isset($data_array['receiver_nocheck']) ) {
					$receiver['id'] = $data_array['receiver_id'];
					$receiver['name'] = $data_array['receiver_name'];
					$receiver['email'] = $data_array['receiver_email'];
				} elseif( isset($data_array['receiver_id']) ) {
					$receiver = $this->_get_user($data_array['receiver_id'], 'userid');
				} elseif( isset($data_array['receiver_email']) ) {
					$receiver = $this->_get_user($data_array['receiver_email'], 'email');
				} else {
					return '-1';
				}
				if( $this->_not_null($receiver) == false ) {
					return '-2';
				}
				$m_rid = $receiver['id'];
				$m_r = $receiver['name'];
				$m_remail = $receiver['email'];
			} else {
				$m_rid = $data_array['receiver_id'];
				$m_r = $data_array['receiver_name'];
				$m_remail = $data_array['receiver_email'];
			}
			$m_rtype = 'customer';
		} elseif( $type == 'c2s' ) {
			if( $data_array['sender_iscustomer'] == '1' ) {
				if( isset($data_array['sender_id']) ) {
					$sender = $this->_get_user($data_array['sender_id'], 'userid');
				} elseif( isset($data_array['sender_email']) ) {
					$sender = $this->_get_user($data_array['sender_email'], 'email');
				} else {
					return '-1';
				}
				if( $this->_not_null($sender) == false ) {
					return '-2';
				}
				$m_sid = $sender['id'];
				$m_s = $sender['name'];
				$m_semail = $sender['email'];
			} else {
				$m_sid = $data_array['sender_id'];
				$m_stype = $data_array['sender_type'];
				$m_s = $data_array['sender_name'];
				$m_semail = $data_array['sender_email'];
				$m_scompany = $data_array['sender_company'];
				$m_stelephone = $data_array['sender_telephone'];
				$m_saddress = $data_array['sender_address'];
			}
			$m_stype = 'customer';
			//
			$receiver = $this->_get_system_owner();
			$m_rid = $receiver['id'];
			$m_rtype = 'supplier';
			$m_r = $receiver['name'];
			$m_remail = $receiver['email'];
		} elseif( $type == 'c2c' ) {
			//
			if( $data_array['sender_iscustomer'] == '1' ) {
				if( isset($data_array['sender_id']) ) {
					$sender = $this->_get_user($data_array['sender_id'], 'userid');
				} elseif( isset($data_array['sender_email']) ) {
					$sender = $this->_get_user($data_array['sender_email'], 'email');
				} else {
					return '-1';
				}
				if( $this->_not_null($sender) == false ) {
					return '-2';
				}
				$m_sid = $sender['id'];
				$m_s = $sender['name'];
				$m_semail = $sender['email'];
			} else {
				$m_sid = $data_array['sender_id'];
				$m_s = $data_array['sender_name'];
				$m_semail = $data_array['sender_email'];
			}
			$m_stype = 'customer';
			//
			if( $data_array['receiver_iscustomer'] == '1' ) {
				if( isset($data_array['receiver_nocheck']) ) {
					$receiver['id'] = $data_array['receiver_id'];
					$receiver['name'] = $data_array['receiver_name'];
					$receiver['email'] = $data_array['receiver_email'];
				} elseif( isset($data_array['receiver_id']) ) {
					$receiver = $this->_get_user($data_array['receiver_id'], 'userid');
				} elseif( isset($data_array['receiver_email']) ) {
					$receiver = $this->_get_user($data_array['receiver_email'], 'email');
				} else {
					return '-1';
				}
				if( $this->_not_null($receiver) == false ) {
					return '-2';
				}
				$m_rid = $receiver['id'];
				$m_r = $receiver['name'];
				$m_remail = $receiver['email'];
			} else {
				$m_rid = $data_array['receiver_id'];
				$m_r = $data_array['receiver_name'];
				$m_remail = $data_array['receiver_email'];
			}
			$m_rtype = 'customer';
		} else {
			//
			$m_sid = $data_array['sender_id'];
			$m_stype = $data_array['sender_type'];
			$m_s = $data_array['sender_name'];
			$m_semail = $data_array['sender_email'];
			$m_scompany = $data_array['sender_company'];
			$m_stelephone = $data_array['sender_telephone'];
			$m_saddress = $data_array['sender_address'];
			//
			$m_rid = $data_array['receiver_id'];
			$m_rtype = $data_array['receiver_type'];
			$m_r = $data_array['receiver_name'];
			$m_remail = $data_array['receiver_email'];
		}
		
		//
		if( isset($data_array['type']) ) {
			$type = $data_array['type'];
		} else {
			$type = $this->_get_query_filter($type);
		}
		
		//
		$query = "insert into pp_messages set messages_subject = '" . $m_subject . "', "
										. " messages_content = '" . $m_content . "', "
										//
										. " messages_type = '" . (int)$type . "', "
										//
										. " messages_senderid = '" . (int)$m_sid . "', "
										//. " messages_sendertype = '" . $m_stype . "', "
										. " messages_sendername = '" . $m_s . "', "
										. " messages_senderemail = '" . $m_semail . "', "
										. " messages_sendercompany = '" . $m_scompany . "', "
										. " messages_sendertelephone = '" . $m_stelephone . "', "
										. " messages_senderaddress = '" . $m_saddress . "', "
										//
										. " messages_receiverid = '" . (int)$m_rid . "', "
										//. " messages_receivertype = '" . $m_rtype . "', "
										. " messages_receivername = '" . $m_r . "', "
										. " messages_receiveremail = '" . $m_remail . "', "
										//
										. " messages_date_added = now(), "
										. " messages_status = '1'"
										;
		
		return $this->adb->insert($query);
	}
	
	//
	function addFeedback($messages_id, $feedback_id) {
		$query = "update pp_messages set messages_feedback = '" . (int)$feedback_id . "' where messages_id = '" . (int)$messages_id . "'";
		
		return $this->adb->update($query);
	}
	
	function getFeedback($feedback_id) {
		$query = "select * from pp_messages where messages_id = '" . (int)$feedback_id . "'";
		
		return $this->adb->getRow($query);
	}
	
	function setMessageReaded($messages_id) {
		$this->adb->update("update pp_messages set messages_isnew = 0 where messages_id = '" . (int)$messages_id . "'");
	}
	
	function checkMessage($value, $type = 'id', $send_receive = '0') {
		$value = func_db_input($value);
		
		if( $type == 'id' || $type == 'messages_id' || $type == 'mid' ) {
			if( $send_receive == '0' ) {//All
				$query = "select count(*) as count from pp_messages where messages_id = '" . (int)$value . "' " 
						. " and (messages_senderid = '" . $this->owner . "' or messages_receiverid = '" . $this->owner . "')";
			} elseif( $send_receive == '1' ) {//Send
				$query = "select count(*) as count from pp_messages where messages_id = '" . (int)$value . "' and messages_senderid = '" . $this->owner . "'";
			} elseif( $send_receive == '2' ) {//Receive
				$query = "select count(*) as count from pp_messages where messages_id = '" . (int)$value . "' and messages_receiverid = '" . $this->owner . "'";
			}
		} elseif( $type == 'link' || $type == 'messages_link' || $type == 'mlink' ) {
			if( $send_receive == '0' ) {
				$query = "select count(*) as count from pp_messages where messages_link = '" . $value . "' " 
						. " and (messages_senderid = '" . $this->owner . "' or messages_receiverid = '" . $this->owner . "')";
			} elseif( $send_receive == '1' ) {
				$query = "select count(*) as count from pp_messages where messages_link = '" . $value . "' and messages_senderid = '" . $this->owner . "'";
			} elseif( $send_receive == '2' ) {
				$query = "select count(*) as count from pp_messages where messages_link = '" . $value . "' and messages_receiverid = '" . $this->owner . "'";
			}
		} else {
			return false;
		}
		
		$row = $this->adb->getRow($query);
		
		if( $row['count'] > 0 ) {
			return true;
		}
		
		return false;
	}
	
	function updateMessage($messages_id, $data_array) {
		$data_array = func_db_input($data_array);
		
		return $this->adb->updateA('pp_messages', $data_array, "messages_id = '" . (int)$messages_id . "'");
	}
	
	//delete
	//	messages_deleted	
	//			1			sender delete
	//			2			receiver delete
	//			3			system delete
	//			
	function deleteMessage($messages_id) {
		return $this->adb->delete("update pp_messages set messages_deleted = 3 where messages_id = '" . (int)$messages_id . "'");
	}
	
	function deleteMessages($filter = '', $folder = '') {
		$query_str = $this->_get_query_str($filter, $folder);
		
		//as receiver
		//set messages delete
		$query = "update pp_messages set messages_deleted = 2 where 1 " 
				. " and messages_receiverid = '" . $this->owner . "' " . $query_str . " and messages_deleted = 0";
		$this->adb->update($query);
		//delete messages where sender delete
		$query = "delete from pp_messages where 1 " 
				. " and messages_receiverid = '" . $this->owner . "' " . $query_str . " and messages_deleted = 1";
		$this->adb->delete($query);
		
		//as sender
		//set messages delete
		$query = "update pp_messages set messages_deleted = 1 where 1 " 
				. " and messages_senderid = '" . $this->owner . "' " . $query_str . " and messages_deleted = 0";
		$this->adb->update($query);
		//delete messages when receiver delete
		$query = "delete from pp_messages where 1 " 
				. " and messages_senderid = '" . $this->owner . "' " . $query_str . " and messages_deleted = 2";
		$this->adb->delete($query);
	}
	
	function deleteUserMessages($users_id, $filter = '', $folder = '') {
		$query_str = $this->_get_query_str($filter, $folder);
		
		//as receiver
		//set messages delete
		$query = "update pp_messages set messages_deleted = 2 where messages_senderid = '" . (int)$users_id . "' " 
				. " and messages_receiverid = '" . $this->owner . "' " . $query_str . " and messages_deleted = 0";
		$this->adb->update($query);
		//delete messages where sender delete
		$query = "delete from pp_messages where messages_senderid = '" . (int)$users_id . "' " 
				. " and messages_receiverid = '" . $this->owner . "' " . $query_str . " and messages_deleted = 1";
		$this->adb->delete($query);
		
		//as sender
		//set messages delete
		$query = "update pp_messages set messages_deleted = 1 where messages_receiverid = '" . (int)$users_id . "' " 
				. " and messages_senderid = '" . $this->owner . "' " . $query_str . " and messages_deleted = 0";
		$this->adb->update($query);
		//delete messages when receiver delete
		$query = "delete from pp_messages where messages_receiverid = '" . (int)$users_id . "' " 
				. " and messages_senderid = '" . $this->owner . "' " . $query_str . " and messages_deleted = 2";
		$this->adb->delete($query);
	}
	
	function deleteUsersMessages($users_ids, $filter = '', $folder = '') {
		$query_str = $this->_get_query_str($filter, $folder);
		
		//as receiver
		//set messages delete
		$query = "update pp_messages set messages_deleted = 2 where messages_senderid in (" . $this->_implode(', ', $users_ids) . ") " 
				. " and messages_receiverid = '" . $this->owner . "' " . $query_str . " and messages_deleted = 0";
		$this->adb->update($query);
		//delete messages where sender delete
		$query = "delete from pp_messages where messages_senderid in (" . $this->_implode(', ', $users_ids) . ") " 
				. " and messages_receiverid = '" . $this->owner . "' " . $query_str . " and messages_deleted = 1";
		$this->adb->delete($query);
		
		//as sender
		//set messages delete
		$query = "update pp_messages set messages_deleted = 1 where messages_receiverid in (" . $this->_implode(', ', $users_ids) . ") " 
				. " and messages_senderid = '" . $this->owner . "' " . $query_str . " and messages_deleted = 0";
		$this->adb->update($query);
		//delete messages when receiver delete
		$query = "delete from pp_messages where messages_receiverid in (" . $this->_implode(', ', $users_ids) . ") " 
				. " and messages_senderid = '" . $this->owner . "' " . $query_str . " and messages_deleted = 2";
		$this->adb->delete($query);
	}
	
	//
	function cleanMessages($filter = '', $folder = '') {
		$query_str = $this->_get_query_str($filter, $folder);
		
		$query = "delete from pp_messages where (messages_senderid = '" . $this->owner . "' or messages_receiverid = '" . $this->owner . "') ";
		$query .= $query_str;
		$this->adb->query($query);
	}
	
	function cleanUserMessages($users_id, $filter = '', $folder = '') {
		$query_str = $this->_get_query_str($filter, $folder);
		
		$query = "delete from pp_messages where (messages_senderid = '" . (int)$users_id . "' or messages_receiverid = '" . (int)$users_id . "') ";
		$query .= $query_str;
		$this->adb->query($query);
	}
	
	//
	function updateMessages($type, $filter = '', $folder = '') {
		$query_str = $this->_get_query_str($filter, $folder);
		
		$query = "update pp_messages set ";
		if( $type == 'draft' ) {
			$query .= "	messages_folder = '3' ";
		} elseif( $type == 'trash' ) {
			$query .= "	messages_folder = '4' ";
		} elseif( $type == 'archive' ) {
			$query .= "	messages_folder = '5' ";
		} else {
			return false;
		}
		$query .= " where (messages_senderid = '" . $this->owner . "' or messages_receiverid = '" . $this->owner . "') " . $query_str;
		
		return $this->adb->update($query);
	}
	
	//default message
	function getMessage($value, $type = 'id') {
		$value = func_db_input($value);
		
		if( $type == 'id' || $type == 'messages_id' || $type == 'mid' ) {
			$query = "select * from pp_messages where messages_id = '" . (int)$value . "' and messages_receiverid = '" . $this->owner . "'";
		} elseif( $type == 'link' || $type == 'messages_link' || $type == 'mlink' ) {
			$query = "select * from pp_messages where messages_link = '" . $value . "' and messages_receiverid = '" . $this->owner . "'";
		} else {
			return false;
		}
		
		return $this->adb->getRow($query);
	}
	
	//receive message
	function getRMessage($value, $type = 'id') {
		$value = func_db_input($value);
		
		if( $type == 'id' || $type == 'messages_id' || $type == 'mid' ) {
			$query = "select * from pp_messages where messages_id = '" . (int)$value . "' and messages_receiverid = '" . $this->owner . "'";
		} elseif( $type == 'link' || $type == 'messages_link' || $type == 'mlink' ) {
			$query = "select * from pp_messages where messages_link = '" . $value . "' and messages_receiverid = '" . $this->owner . "'";
		} else {
			return false;
		}
		
		return $this->adb->getRow($query);
	}
	
	//sent message
	function getSMessage($value, $type = 'id') {
		$value = func_db_input($value);
		
		if( $type == 'id' || $type == 'messages_id' || $type == 'mid' ) {
			$query = "select * from pp_messages where messages_id = '" . (int)$value . "' and messages_senderid = '" . $this->owner . "'";
		} elseif( $type == 'link' || $type == 'messages_link' || $type == 'mlink' ) {
			$query = "select * from pp_messages where messages_link = '" . $value . "' and messages_senderid = '" . $this->owner . "'";
		} else {
			return false;
		}
		
		return $this->adb->getRow($query);
	}
	
	//default messages
	function getMessages($filter = '', $folder = '') {
		$query_str = $this->_get_query_str($filter, $folder);
		
		$query = "select * from pp_messages where messages_receiverid = '" . $this->owner . "' " 
				. $query_str . " order by messages_id desc " . $this->getQueryLimitStr();
		
		return $this->adb->getRows($query);
	}
	
	function getMessagesTotal($filter = '', $folder = '') {
		$query_str = $this->_get_query_str($filter, $folder);
		
		$query = "select count(*) as count from pp_messages where messages_receiverid = '" . $this->owner . "' " 
				. $query_str . " order by messages_id desc " . $this->getQueryLimitStr();
		
		return $this->adb->getCount($query);
	}
	
	//both messages
	function getBMessages($filter = '', $folder = '') {
		$query_str = $this->_get_query_str($filter, $folder);
		
		$query = "select * from pp_messages where (messages_receiverid = '" . $this->owner . "' or messages_senderid = '" . $this->owner . "') " 
				. $query_str . " order by messages_id desc " . $this->getQueryLimitStr();
		
		return $this->adb->getRows($query);
	}
	
	function getBMessagesTotal($filter = '', $folder = '') {
		$query_str = $this->_get_query_str($filter, $folder);
		
		$query = "select count(*) as count from pp_messages where (messages_receiverid = '" . $this->owner . "' or messages_senderid = '" . $this->owner . "') " 
				. $query_str . " order by messages_id desc " . $this->getQueryLimitStr();
		
		return $this->adb->getCount($query);
	}
	
	//common messages
	function getCMessages($filter = '', $folder = '') {
		$query_str = $this->_get_query_str($filter, $folder);
		
		$query = "select * from pp_messages where messages_receiverid = '" . $this->owner . "' " 
				. $query_str . " order by messages_id desc " . $this->getQueryLimitStr();
		
		return $this->adb->getRows($query);
	}
	
	function getCMessagesTotal($filter = '', $folder = '') {
		$query_str = $this->_get_query_str($filter, $folder);
		
		$query = "select count(*) as count from pp_messages where messages_receiverid = '" . $this->owner . "' " 
				. $query_str . " order by messages_id desc " . $this->getQueryLimitStr();
		
		return $this->adb->getCount($query);
	}
	
	//receive messages
	function getRMessages($filter = '', $folder = '') {
		$query_str = $this->_get_query_str($filter, $folder);
		
		$query = "select * from pp_messages where messages_receiverid = '" . $this->owner . "' " 
				. $query_str . " order by messages_id desc " . $this->getQueryLimitStr();
		
		return $this->adb->getRows($query);
	}
	
	function getRMessagesTotal($filter = '', $folder = '') {
		$query_str = $this->_get_query_str($filter, $folder);
		
		$query = "select count(*) as count from pp_messages where messages_receiverid = '" . $this->owner . "' " 
				. $query_str . " order by messages_id desc " . $this->getQueryLimitStr();
		
		return $this->adb->getCount($query);
	}
	
	//sent messages
	function getSMessages($filter = '', $folder = '') {
		$query_str = $this->_get_query_str($filter, $folder);
		
		$query = "select * from pp_messages where messages_senderid = '" . $this->owner . "' " 
				. $query_str . " order by messages_id desc " . $this->getQueryLimitStr();
		
		return $this->adb->getRows($query);
	}
	
	function getSMessagesTotal($filter = '', $folder = '') {
		$query_str = $this->_get_query_str($filter, $folder);
		
		$query = "select count(*) as count from pp_messages where messages_senderid = '" . $this->owner . "' " 
				. $query_str . " order by messages_id desc " . $this->getQueryLimitStr();
		
		return $this->adb->getCount($query);
	}
	
	//
	function getLatestMessages($filter = '', $folder = 'inbox') {
		return $this->getMessages($filter, $folder);
	}
	
	//stats
	function getStatistics() {
		$stats = array();
		$stats['messages'] = $this->adb->getCountA('pp_messages', "messages_status = 1");
		
		return $stats;
	}
	
	//black
	function getBlacks() {
		
	}
	
	function addBlack() {
		
	}
	
	function deleteBlack() {
		
	}
	
	//private
	function _get_user($value, $type = 'username') {
		$value = func_db_input($value);
		
		$query = "select users_id as id, users_name as name, users_email_address as email from pp_users where ";
		if( $type == 'username' || $type == 'name' || $type == 'users_name' ) {
			$query .= " users_name = '" . $value . "'";
		} elseif( $type == 'email_address' || $type == 'email' || $type == 'users_email_address' ) {
			$query .= " users_name = '" . $value . "'";
		} elseif( $type == 'userid' || $type == 'id' || $type == 'users_id' ) {
			$query .= " users_id = '" . (int)$value . "'";
		} else {
			return false;
		}
		
		return $this->adb->getRow($query);
	}
	
	function _filter_folder($folder) {
		if( $folder == '' ) {
			return 0;
		}
		switch($folder) {
			case 'inbox':
				$f = '1';
				break;
			case 'outbox':
				$f = '2';
				break;
			case 'draft':
				$f = '3';
				break;
			case 'trash':
				$f = '4';
				break;
			case 'archive':
				$f = '5';
				break;
			default:
				$f = '0';
				break;
		}
		
		return $f;
	}
	
	function _get_supplier_owner() {
		return array('id' => 0, 'name' => '', 'email' => '');
	}
	
	function _get_system_owner() {
		$user = array();
		$user['id'] = CONST_MESSAGE_SYSTEM_ID;
		$user['name'] = CONST_MESSAGE_SYSTEM_NAME;
		$user['email'] = CONST_MESSAGE_SYSTEM_EMAIL;
		
		return $user;
	}
	
	//
	function _get_query_str($filter = '', $folder = '') {
		$query_str = "";
		$folder = $this->_filter_folder($folder);
		if( $folder > 0 ) {
			$query_str .= " and messages_folder = '" . (int)$folder . "' ";
		}
		if( !is_array($filter) ) {
			$filter = $this->_get_query_filter($filter);
		}
		if( is_array($filter) ) {
			if( isset($filter['keyword']) ) {
				$query_str .= " and (";
				$query_str .= " messages_subject like '%" . $filter['keyword'] . "%' or messages_content like '%" . $filter['keyword'] . "%' ";
				$query_str .= " or messages_sender like '%" . $filter['keyword'] . "%' or messages_receiver like '%" . $filter['keyword'] . "%' ";
				$query_str .= " ) ";
			}
			if( isset($filter['inbox']) ) {
				$query_str .= " and messages_folder = 1 ";
			}
			if( isset($filter['outbox']) ) {
				$query_str .= " and messages_folder = 2 ";
			}
			if( isset($filter['draft']) ) {
				$query_str .= " and messages_folder = 3 ";
			}
			if( isset($filter['trash']) ) {
				$query_str .= " and messages_folder = 4 ";
			}
			if( isset($filter['archive']) ) {
				$query_str .= " and messages_folder = 5 ";
			}			
			if( isset($filter['folder']) ) {
				$query_str .= " and messages_folder in (" . $this->_implode(',', $filter['folder']) . ") ";
			}
			if( isset($filter['type']) ) {
				$query_str .= " and messages_type = '" . (int)$filter['type'] . "' ";
			}
			if( isset($filter['deleted']) ) {
				$query_str .= " and messages_deleted > 0 ";
			} elseif( isset($filter['nodeleted']) ) {
				$query_str .= " and messages_deleted = 0 ";
			}
			if( isset($filter['isnew']) ) {
				$query_str .= " and messages_isnew = 1 ";
			}
			if( isset($filter['read']) ) {
				$query_str .= " and messages_isnew = 0 ";
			}
			if( isset($filter['from']) ) {
				$query_str .= " and messages_senderid = '" . (int)$filter['from'] . "' ";
			}
			if( isset($filter['to']) ) {
				$query_str .= " and messages_receiverid = '" . (int)$filter['to'] . "' ";
			}
			if( isset($filter['ids']) ) {
				$query_str .= " and messages_id in (" . $this->_implode(',', $filter['ids']) . ") ";
			}
		}
		
		return $query_str;
	}
	
	function _get_query_filter($filter) {
		$type = 0;
		switch($filter) {
			case 'invites':
				$type = 1;
				break;
			case 'articles':
				$type = 2;
				break;
			case 'albums':
				$type = 3;
				break;
			case 'products':
				$type = 4;
				break;
			case 'videos':
				$type = 5;
				break;
			case 'comments':
				$type = 6;
				break;
			case 'f1':
				$type = 7;
				break;
			case 'f2':
				$type = 8;
				break;
			case 'f3':
				$type = 9;
				break;
			case 's2c':
				$type = 11;
				break;
			case 'c2s':
				$type = 12;
				break;
			case 'c2c':
				$type = 13;
				break;
			case 'order':
				$type = 21;
				break;
			default:
				$type = 0;
				break;
		}
		
		return array('type' => $type);
	}
}
//
?>